package com.stone.drawable;

import com.stone.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
/**
 * 这是一个画画的小示例，但是这个程序是有问题的
 * 将书上的一些代码，注释掉后可以解决这个问题了。
 * 
 * */
public class DrawableImageActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	LinearLayout layout=new LinearLayout(this);
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.green);
        DrawableImageView mdView=new DrawableImageView(this, bitmap);
        layout.addView(mdView);
        setContentView(layout);
    }
    
    public class DrawableImageView extends View{
       private Bitmap mBitmap;
       private Bitmap pic;
       private Canvas mCanvas;
       private final Paint mPaint;
       private int a=255;
       private int r=255;
       private int g=255;
       private int b=255;
       private float width=4;
		public DrawableImageView(Context context,Bitmap img) {
			super(context);
			pic=img;
			mPaint=new Paint();
			mPaint.setAntiAlias(true);
			mPaint.setARGB(a, r, g, b);
			Bitmap newBitmap=Bitmap.createBitmap(img.getWidth(),img.getHeight(),Bitmap.Config.RGB_565);
			Canvas newCanvas=new Canvas();
			newCanvas.setBitmap(newBitmap);
			if(img!=null){
				newCanvas.drawBitmap(img, 0, 0,null);
			}
			mBitmap=newBitmap;
			mCanvas=newCanvas;
			mCanvas.setBitmap(mBitmap);
		}
    	public DrawableImageView(Context c,Bitmap img,int a,int r,int g,int b){
    		this(c,img);
    	    setColor(a, r, g, b);
    	}
    	public DrawableImageView(Context c,Bitmap img,int a,int r,int g,int b,float w){
    		 this(c,img);
    		 width=w;
    	}
       private void setColor(int alpha,int red,int green,int blue){
    	    a=alpha;
    	    r=red;
    	    g=green;
    	    b=blue;
    	    mPaint.setARGB(a, r, g, b);
       }
       private void setWidth(float w){
    	    width=w;
       }
       private Bitmap getBitmap(){
    	   return mBitmap;
       }
       private void undo(){
    	   mCanvas.drawBitmap(pic,0,0,null);
    	   //强制重绘
    	   invalidate();
       }
       float scaleX;
       float scaleY;
       float scale;
       @Override
    	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    		// TODO Auto-generated method stub
    		super.onSizeChanged(w, h, oldw, oldh);
    		scaleX=(float)w/mBitmap.getWidth();
    		scaleY=(float)h/mBitmap.getHeight();
    		scale=scaleX>scaleY?scaleY:scaleX;
    	}
       @Override
    	protected void onDraw(Canvas canvas) {
    		// TODO Auto-generated method stub
    		super.onDraw(canvas);
    		if(mBitmap!=null){
    			 Matrix matrix=new Matrix();
    			 matrix.postScale(scale, scale);
    			 canvas.drawBitmap(mBitmap, matrix,null);
    		}
    	}
       float lastX;
       float lastY;
       @Override
    	public boolean onTouchEvent(MotionEvent event) {
    		// TODO Auto-generated method stub
    	    mPaint.setStrokeWidth(width/scale);
    	    float curX=event.getX()/scale;
    	    float curY=event.getY()/scale;
    	    switch(event.getAction()){
//    	    case MotionEvent.ACTION_DOWN:
//    	    	  mCanvas.drawCircle(curY, curX, width/2/scale, mPaint);
//    	    	  break;
    	    case MotionEvent.ACTION_MOVE:
    	    	mCanvas.drawLine(lastX, lastY, curX, curY, mPaint);
    	    	//mCanvas.drawCircle(curY, curX, width/2/scale, mPaint);
    	    	break;
    	    //case MotionEvent.ACTION_CANCEL:
    	    case MotionEvent.ACTION_UP:
    	    	mCanvas.drawLine(lastX, lastY, curX, curY, mPaint);
    	    	//mCanvas.drawCircle(curY, curX, width/2/scale, mPaint);
    	    	break;
    	    }
    	    lastX=curX;
    	    lastY=curY;
    	    invalidate();
    		return true;
    	}
    }
    
}
